

package com.xgit.iov.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xgit.iov.admin.service.SysMenuService;
import com.xgit.iov.common.core.entity.IovUser;
import com.xgit.iov.common.core.entity.SysMenu;
import com.xgit.iov.common.core.util.R;
import com.xgit.iov.common.core.util.SecurityUtils;
import com.xgit.iov.common.log.annotation.LogAnnotation;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author lengleng
 * @date 2017/10/31
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/menu")
@Api(value = "menu", tags = "菜单管理模块")
public class MenuController {

	private final SysMenuService sysMenuService;

	/**
	 * 返回当前用户的树形菜单集合
	 *
	 * @param parentId 父节点ID
	 * @return 当前用户的树形菜单
	 */
	@GetMapping
	public R getUserMenu(Long parentId) {

		// 获取符合条件的菜单
		Set<SysMenu> all = new HashSet<>();
		IovUser iovUser = SecurityUtils.getUser();
		if (iovUser.isAdmin()){
			all.addAll(sysMenuService.list(new QueryWrapper<SysMenu>()
				.orderByAsc("sort")));
		} else {
			SecurityUtils.getRoles()
				.forEach(roleId -> all.addAll(sysMenuService.findMenuByRoleId(roleId)));
		}

		return R.ok(sysMenuService.filterMenu(all, parentId));
	}

	/**
	 * 返回树形菜单集合
	 * @return 树形菜单
	 */
	@GetMapping(value = "/tree")
	public R getTree(SysMenu sysMenu) {
		return R.ok(sysMenuService.treeMenu(sysMenu));
	}

	/**
	 * 返回角色的菜单集合
	 *
	 * @param roleId 角色ID
	 * @return 属性集合
	 */
	@GetMapping("/tree/{roleId}")
	public R getRoleTree(@PathVariable Long roleId) {
		List<SysMenu> menuVOList =  sysMenuService.findMenuByRoleId(roleId);
		return R.ok(menuVOList.stream().map(menuVO -> String.valueOf(menuVO.getId())).collect(Collectors.toList()));
	}

	/**
	 * 返回数据权限的树形菜单集合
	 */
	@GetMapping(value = "/dataRuleTree")
	public R getDataRuleTree() {
		return R.ok(sysMenuService.dataRuleTree());
	}


	/**
	 * 返回数据权限的菜单集合
	 *
	 * @param roleId 角色ID
	 * @return 属性集合
	 */
	@GetMapping("/dataRuleTree/{roleId}")
	public R getRoleDataRuleTree(@PathVariable Long roleId) {
		List<SysMenu> menuVOList =  sysMenuService.findDataRuleMenuByRoleId(roleId);
		return R.ok(menuVOList.stream().map(menuVO -> String.valueOf(menuVO.getId())).collect(Collectors.toList()));
	}

	/**
	 * 通过ID查询菜单的详细信息
	 *
	 * @param id 菜单ID
	 * @return 菜单详细信息
	 */
	@GetMapping("/{id}")
	public R getById(@PathVariable Long id) {
		return R.ok(sysMenuService.getById(id));
	}

	/**
	 * 新增菜单
	 *
	 * @param sysMenu 菜单信息
	 * @return 含ID 菜单信息
	 */
	@LogAnnotation("新增菜单")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('sys_menu_add')")
	public R save(@Valid @RequestBody SysMenu sysMenu) {
		sysMenuService.save(sysMenu);
		return R.ok(sysMenu);
	}

	/**
	 * 删除菜单
	 *
	 * @param id 菜单ID
	 * @return success/false
	 */
	@LogAnnotation("删除菜单")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('sys_menu_del')")
	public R removeById(@PathVariable Long id) {
		return R.ok(sysMenuService.removeMenuById(id));
	}

	/**
	 * 更新菜单
	 *
	 * @param sysMenu
	 * @return
	 */
	@LogAnnotation("更新菜单")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('sys_menu_edit')")
	public R update(@Valid @RequestBody SysMenu sysMenu) {
		return R.ok(sysMenuService.updateMenuById(sysMenu));
	}

}
